import { getCurrentInstance, ref } from 'vue'
import { LoginParam } from '@/api/system/user/UserModel'
import { userStore } from '@/store/user'
import { useRouter } from 'vue-router'

export default function useLogin(loginModel: LoginParam) {
    const { proxy } = getCurrentInstance() as any
    const store = userStore()
    const router = useRouter()

    const isCanSubmit = ref(true)

    const login = async () => {
        // 表单验证
        proxy.$refs.loginFormRef.validate(async (valid: boolean) => {
            if (valid) {
                store.login(loginModel).then((res) => {
                    console.log('登录成功：', res)
                    if (res) {
                        // 跳转到首页
                        router.push({ path: '/' })
                    }
                })
            } else {
                console.log('error submit!!')
                return false
            }
        })
    }

    const install = async () => {
        router.push({ path: '/install' })
    }

    return {
        isCanSubmit,
        login,
        install
    }
}
